#include "zj_logger.h"
#include "common.hpp"
#include "WelcomePage.hpp"
#include "PageManager.hpp"
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#define is_quit_cmd(event) ( (SDLK_AC_BACK == event.key.keysym.sym || SDLK_ESCAPE == event.key.keysym.sym || SDLK_q == event.key.keysym.sym) ? true : false )


WelcomePage::WelcomePage()
{
    page_name = "WelcomePage";

    ok = false;
}

WelcomePage::~WelcomePage()
{
}

void WelcomePage::init()
{
    btn_done.init(app_state, "完成");
    
    DIR * dp;
    struct dirent *filename;
    SDL_Surface* sf_list[10] = {0}; // Surface列表
    int list_i = 0;                 //个数

    dp = opendir(app_state->save_path.c_str());
    if (!dp) {
        zj_logger_e("init", "打开文件夹失败");
        return;
    }
    //加载文件
    while (1) {
        filename = readdir(dp);
        if (!filename) break;
        // 判断扩展名
        if (strstr(filename->d_name, ".jpg") != 0
            || strstr(filename->d_name, ".JPG") != 0
            || strstr(filename->d_name, ".jpeg") != 0
            || strstr(filename->d_name, ".JPEG") != 0
            || strstr(filename->d_name, ".png") != 0
            || strstr(filename->d_name, ".PNG") != 0
            ) {
            zj_logger_i("init", "文件名: %s", filename->d_name);
            sf_list[list_i] = IMG_Load((app_state->save_path + filename->d_name).c_str());
            if (!sf_list[list_i]) {
                zj_logger_e("init", "Load错误:%s", SDL_GetError());
                return;
            }
            list_i++;
        }
    }
    closedir(dp);
    if (list_i<=0)
        return;
    
    // 计算结果大小w,h
    int w=0, h=0;
    for (int i=0; i<list_i; i++) {
        w += sf_list[i]->w;
        if (sf_list[i]->h > h)
            h = sf_list[i]->h;
    }
    w += list_i-1;
    SDL_Surface* result = SDL_CreateRGBSurface(0, w, h, 32, 0, 0, 0, 0);
    SDL_FillRect(result, NULL, SDL_MapRGB(result->format, 0x00, 0x00, 0x00));

    // 拼接
    int last_x = 0;
    for (int i=0; i<list_i; i++) {
        if (i!=0)
            last_x += sf_list[i-1]->w + 1;
        SDL_Rect rect = { last_x, 0, sf_list[i]->w, sf_list[i]->h };
        SDL_BlitSurface(sf_list[i], 0, result, &rect);
    }
    
    //保存
    {
        srand(time(0));
        char buf[100] = {'\0'};
        sprintf(buf, "%05d.jpg", rand()%100000);
        zj_logger_i("init", "随机文件名: %s", buf);
        IMG_SaveJPG(result, (app_state->save_path + buf).c_str(), 30);
        ok = true;
    }

    // 释放空间
    for (int i=0; i<list_i; i++) {
        SDL_FreeSurface(sf_list[i]);
    }
    SDL_FreeSurface(result);
    zj_logger_i("init", "执行完毕");
}

void WelcomePage::update()
{
    btn_done.update();
}

void WelcomePage::handle(const SDL_Event& event)
{
    // 退出
    if (event.type == SDL_KEYUP)
        if (is_quit_cmd(event))
            app_state->page_manager->request_exit();
    if (event.type == SDL_QUIT)
        app_state->page_manager->request_exit();

    if (event.type == SDL_MOUSEMOTION) {
        mp.x = event.motion.x;
        mp.y = event.motion.y;
    }
    btn_done.handle(event);
    if (btn_done.clicked)
        app_state->page_manager->request_exit();
}

void WelcomePage::draw(const SDL_Rect& canvas)
{
    SDL_SetRenderDrawColor(app_state->renderer, 230, 230, 230, 255);
    if (!ok)
        SDL_SetRenderDrawColor(app_state->renderer, 230, 0, 0, 255);
    SDL_RenderClear(app_state->renderer);
    
    btn_done.w = canvas.w/2;
    btn_done.h = canvas.w/2*0.3;
    btn_done.x = canvas.x + canvas.w*0.5 - btn_done.w*0.5;
    btn_done.y = canvas.y + canvas.h*0.5 - btn_done.h*0.5;
    btn_done.draw();
}
